<?php


namespace app\manage\controller;

use think\Log;

class Plan extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $plan_name = input('plan_name','');
        $status = input('status','');

        $map = [];
        if($plan_name != ''){
            $map['plan_name'] = ['like','%'.$plan_name.'%'];
        }

        if($status != 0){
            $map['status'] = $status;
        }

        $infos = model('Plan')->getInfosByMap($map, '', false);

        $channels = $this->getChannelArray();

        return $this->fetch('',[
            'infos' => $infos,
            'channels' => $channels,
            'plan_name' => $plan_name,
            'status' => $status,
        ]);
    }

    // 媒体线索接入地址
    public function clue_url()
    {
        $id = input('id');
        $account = model('Channel')->getArrayByMap(['id'=>$id],'clue_url,other_url,docx_url','',false);
        return self::showJsonReturnCodeData('1001',$account,'');
    }

    // 线索token
    public function get_access_token()
    {
        $plan_name = input('plan_name');
        $plan = model('Plan')->where(['plan_name'=>$plan_name])->find();
        if($plan){
            return self::showReturnCode(1021,'方案名称重复');
        }

        $access_token = md5($plan_name.mt_rand(1000,9999));
        $saveData = [
            'plan_name' => $plan_name,
            'access_token' => $access_token
        ];

        try{

            $token = model('ClueToken')->where(['plan_name'=>$plan_name])->find();
            if($token){
                $saveData['id'] = $token['id'];
            }

            $this->editData(false,'','ClueToken',$saveData);

            return [
                'code' => '1001',
                'msg' => 'token生成成功',
                'data' => ['access_token' => $access_token],
            ];

        }catch (\Exception $e){
            Log::error($e->getMessage());
            return self::showReturnCode(1011,$e->getMessage());
        }

    }

    public function edit()
    {
        $id = input('id');
        if(!$id) return self::showReturnCode(1002,'id 错误');
        $user = model('Plan')->getInfoByMap(['id'=>$id]);
        return self::showJsonReturnCodeData('1001',$user,'');
    }

    public function save()
    {
        if(request()->isAjax()){
            try{

                $id = input('post.id');
                $save_data['plan_name'] = input('post.plan_name');
                $save_data['channel_id'] = input('post.channel_id');
                $save_data['token'] = input('post.access_token');
                $save_data['url'] = input('post.token_url');
                $access_token = input('post.access_token');

                $token = model('ClueToken')->where(['plan_name'=>$save_data['plan_name'],'access_token'=>$access_token])->find();
                if($token){
                    if($token['status'] == 0){
                        return self::showReturnCode(1011,'平台线索未调试成功，请设置完成后重试');
                    }
                }else{
                    return self::showReturnCode(1011,$save_data['plan_name'].'未生成线索access_token，请刷新重试');
                }

                if(isset($id) && $id){
                    $save_data['id'] = $id;
                }
                $result = $this->editData(false,'','Plan',$save_data);
                if($result['code'] == 1001){
                    if(isset($id) && $id){
                        $result['msg'] = '修改方案成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'修改方案【'.$save_data['plan_name'].'】成功');
                    }else{
                        $result['msg'] = '添加方案成功';
                        $this->RecordSystemLog($this->uid,$this->behavior,'添加方案【'.$save_data['plan_name'].'】成功');
                    }
                }

                return $result;
            }catch (\Exception $e){
                Log::error($e->getMessage());
                return self::showReturnCode(1011,$e->getMessage());
            }
        }else{
            return self::showReturnCode(1002);
        }
    }

    public function status()
    {
        if($this->request->isAjax()){
            $id = input('post.id');
            $res = model('Plan')->save(['status'=>input('post.status')],['id'=>$id]);
            if($res){
                $this->RecordSystemLog($this->uid,$this->behavior,'状态操作成功,操作ID: '.$id);
                return $this->showReturnCode(1001);
            }else{
                return $this->showReturnCode(1009,'操作失败');
            }
        }else{
            return $this->showReturnCode(1002);
        }
    }
}